package at.fhooe.stefanirndorfer.util;

import java.awt.Point;
import java.awt.geom.Point2D;

public class Vector2D extends Point2D.Double {

	private static final long	serialVersionUID	= -1454099491358300525L;

	public Vector2D(double[] coords) {
		super(coords[0], coords[1]);
	}

	public Vector2D(double x, double y) {
		super(x, y);
	}

	/*
	 * angle 0 is a vector pointing to the right
	 */
	public Vector2D(double ang) {
		super(Math.sin(ang), -Math.cos(ang));
	}

	@Override
	public Vector2D clone() {
		return new Vector2D(x, y);
	}

	public Vector2D normalize() {
		double l = Math.sqrt(x * x + y * y);
		if (0 != l) {
			x /= l;
			y /= l;
		}
		return this;
	}

	public Vector2D invert() {
		return scale(-1);
	}

	public Vector2D scale(double i) {
		x *= i;
		y *= i;
		return this;
	}

	public Vector2D add(Point2D other) {
		x += other.getX();
		y += other.getY();
		return this;
	}

	public Vector2D sub(Point2D other) {
		x -= other.getX();
		y -= other.getY();
		return this;
	}

	public double dot(Vector2D other) {
		return this.x * other.x + this.y * other.y;
	}

	public Vector2D rotate(double ang) {
		double cos = Math.cos(ang);
		double sin = Math.sin(ang);
		x = x * cos - y * sin;
		y = x * sin + y * cos;
		return this;
	}

	public Point toPoint() {
		return new Point((int) x, (int) y);
	}

}
